package com.algoshow.controller;

import com.algoshow.model.ArrayData;
import com.algoshow.model.SortData;
import com.algoshow.view.SecondMenu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class ArrayPage extends BasePage {

    private List<ArrayData> arr;
    private Integer index;

    private Integer value;

    // 监控的变量
    public Integer i = 0;
    public Integer j = 0;

    private static final int SIZE = 80;

    public ArrayPage(List<ArrayData> arr, int index, int value, String algoName) throws HeadlessException {
        this.algoName = algoName;
        this.arr = arr;
        this.index = index;
        this.value = value;
        firstMenuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer != null && timer.isAlive()) {
                    timer.stop();
                }
                dispose();
                new SecondMenu().showTest4();
            }
        });
    }

    @Override
    public void drawAlgo(Graphics g) {
        drawArray(g);
    }

    @Override
    public void drawParam(Graphics g) {
        // 坐标
        int x = paramX, y = paramY;
        System.out.println("test" + " " + i + " " + j);
        g.drawString("i = ", x, y);
        g.drawString(i.toString(), x + 35, y);
        g.drawString("j = ", x, y + 40);
        g.drawString(j.toString(), x + 35, y + 40);
    }

    private void drawArray(Graphics g) {
        int x = startX;
        int y = startY  - 100;
        for (int i = 0; i < arr.size(); i++) {
            g.setColor(arr.get(i).getColor());
            g.fillRect(x, y, SIZE, SIZE);
            g.setColor(Color.BLACK);
            g.drawRect(x, y, SIZE, SIZE);

            if (arr.get(i).getValue() != null) {
                g.drawString(String.valueOf(arr.get(i).getValue()), x + 30, y + 40);
            }
            x += SIZE;
        }
    }

    @Override
    public void startThread() {
        timer = new Thread() {
            @Override
            public void run() {
                switch (algoName) {
                    case "arrayInsert":
                        arrayInsert();
                        break;
                    case "arrayDelete":
                        arrayDelete();
                        debug();
                        break;
                    default:
                        timer.stop();
                }
            }
        };
        timer.start();
    }

    // 顺序表第i位插入
    private void arrayInsert() {
        index--;
        if (index > arr.size()) {
            return;
        }
        arr.add(new ArrayData());
        j = arr.size() - 1;
        for (i = arr.size() - 2; i >= 0; i--, j--) {
            render(1, 300);
            render(2, 300);
            arr.get(j).setColor(GREEN);
            arr.get(i).setColor(GREEN);
            render();
            render(3);
            arr.get(j).setValue(arr.get(i).getValue());
            render(4, 300);
            if (i == index) {
                arr.get(i).setValue(value);
                arr.get(i).setColor(yellow);
                arr.get(i + 1).setColor(blur);
                render(5);
                render(6);
                break;

            }
            arr.get(j).setColor(blur);
            arr.get(i).setColor(blur);
        }
    }
    // 顺序表第i位删除
    private void arrayDelete() {
        // 1 2 3 4 5
        // 0 1 2 3 4
        render(1, 300);

        int pre = arr.get(arr.size() - 1).getValue();
        for (i = arr.size() - 2; i >= 0; i--) {
            render(2, 300);
            arr.get(i).setColor(GREEN);
            arr.get(i + 1).setColor(GREEN);
            int temp = arr.get(i).getValue();
            render(3);
            arr.get(i).setValue(pre);
            render(4);
            arr.get(i).setColor(blur);
            arr.get(i + 1).setColor(blur);
            pre = temp;
            render(5);
            if (i == index) {
                render(6);
                break;
            }
        }
        arr.get(arr.size() - 1).setValue(null);
        render();
    }

    private void debug() {
        for (ArrayData arrayData : arr) {
            System.out.println(arrayData);
        }
    }
}
